package download.itydl.com.sinlethread;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import java.util.ArrayList;

import download.itydl.com.sinlethread.mutilthread.adapter.MultiDownloadAdapter;
import download.itydl.com.sinlethread.mutilthread.bean.FileInfo;
import download.itydl.com.sinlethread.mutilthread.services.DownloadService;


/**
 * 多线程下载入口
 *
 * @author howard
 * ListView的一条Item对应一个文件、对应一个url、对应一个下载任务DownloadTask   ，一个下载任务又对应多个下载线程
 */
public class MultiActivity extends AppCompatActivity {
    private static final String TAG = "DownloadService2";

    private ListView mListView;
    private ArrayList<FileInfo> mFileInfoList = new ArrayList<>();
    private MyBroadcastReceiver receiver;
    private MultiDownloadAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi);
        mListView = findViewById(R.id.listView);
        initData();
        adapter = new MultiDownloadAdapter(this, mFileInfoList);
        mListView.setAdapter(adapter);

        initReceiver();
    }

    private void initReceiver() {
        receiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownloadService.ACTION_UPDATE);
        filter.addAction(DownloadService.ACTION_FINISHED);
        registerReceiver(receiver, filter);
    }

    /**
     * 下载多个文件，每个文件的下载又是多线程
     */
    private void initData() {
        FileInfo fileInfo = new FileInfo(0,
                "http://172.18.198.175:8080/QQ.apk",
                "QQ.apk",
                0,
                0);
        FileInfo fileInfo1 = new FileInfo(1,
                "http://172.18.198.175:8080/imoc.apk",
                "imoc.apk",
                0,
                0);
        FileInfo fileInfo3 = new FileInfo(2,
                "http://172.18.198.175:8080/ydl.apk",
                "ydl.apk",
                0,
                0);
        FileInfo fileInfo4 = new FileInfo(3,
                "http://172.18.198.175:8080/sxx.apk",
                "sxx.apk",
                0,
                0);
        FileInfo fileInfo5 = new FileInfo(4,
                "http://172.18.198.175:8080/yang.apk",
                "yang.apk",
                0,
                0);
        mFileInfoList.add(fileInfo);
        mFileInfoList.add(fileInfo1);
        mFileInfoList.add(fileInfo3);
        mFileInfoList.add(fileInfo4);
        mFileInfoList.add(fileInfo5);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(DownloadService.ACTION_FINISHED)) {
                FileInfo fileInfo = (FileInfo) intent.getSerializableExtra("fileinfo");
                // 下载完毕，进度清为0
                adapter.upDataProgress(fileInfo.getId(), 0);
                Log.e(TAG, "Activity，下载完毕：id==" + fileInfo.getId());
            } else if (action.equals(DownloadService.ACTION_UPDATE)) {
                long finished = intent.getLongExtra("finished", 0);
                int position = intent.getIntExtra("id", 0);
                Log.e(TAG, "Activity，进度更新：id==" + position + "，进度=" + finished);
                // 刷新ListView
                adapter.upDataProgress(position, finished);
            }
        }
    }
}
